package com.fingard.dsp.bank.directbank.icbc41;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ICBC41RealTime extends ICBC41Base implements IRealTime {

    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            ICBC41Header bankHeader = new ICBC41Header();
            bankHeader.seq = p_req9108.singleDetail.askNumber;
            bankHeader.tx_code = "80001";
            bankHeader.tx_time = Format.DateTime14Format.format(p_req9108.singleDetail.getPayDateTime());
            bankHeader.sysid = getBankConfig().getMapValueByKey("sysid", p_req9108.singleDetail.recAct);

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(bankHeader, xmlWriter);
            xmlWriter.writeElementString("BUSICODE", p_req9108.singleDetail.authCode);//客户编号
            md5Map.put("BUSICODE", p_req9108.singleDetail.authCode);
            xmlWriter.writeElementString("ACC_NO", p_req9108.singleDetail.payAct);//卡号
            md5Map.put("ACC_NO", p_req9108.singleDetail.payAct);
            xmlWriter.writeElementString("ACC_NAME", p_req9108.singleDetail.payName);//姓名
            md5Map.put("ACC_NAME", p_req9108.singleDetail.payName);
            String payType = "物流款".equals(p_req9108.singleDetail.purpose) ? "2" : "1";
            xmlWriter.writeElementString("PAYTYPE", payType);//扣费类型 1-货款，2-物流款
            md5Map.put("PAYTYPE", payType);
            String amount = StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount);
            xmlWriter.writeElementString("AMOUNT", amount);//金额 以分为单位
            md5Map.put("AMOUNT", amount);
            xmlWriter.writeElementString("MOBILE", "true".equals(getBankConfig().getValueAsString("canMobileEmpty")) ? "" : p_req9108.singleDetail.cellPhone);//手机号
            md5Map.put("MOBILE", "true".equals(getBankConfig().getValueAsString("canMobileEmpty")) ? "" : p_req9108.singleDetail.cellPhone);
            xmlWriter.writeElementString("RSV", p_req9108.singleDetail.abstractATS);//备注
            md5Map.put("RSV", p_req9108.singleDetail.abstractATS);

            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpBkReader.getFirstText("/TXN/HEAD/RSP_DESC");
                    retDetail.transState = tmpBkReader.getFirstText("/TXN/HEAD/RSP_CD");
                    retDetail.payInfoCode = retDetail.transState;
                    retDetail.reqSeqID = p_req9108.singleDetail.askNumber;//tmpBkReader.getFirstText("/TXN/HEAD/SEQ");
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            retDetail.respInfo = ex.getMessage();
            retDetail.respCode = "-1";
            ex.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);

        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        retDetail.respCode = "";
        try {
            ICBC41Header bankHeader = new ICBC41Header();
            bankHeader.seq = p_req9144.reqHeader.transSeqID;
            bankHeader.tx_code = "80002";
            bankHeader.sysid = getBankConfig().getMapValueByKey("sysid", p_req9144.recAct);

            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(bankHeader, xmlWriter);
            xmlWriter.writeElementString("ORG_SEQ", p_req9144.askNumber);//原交易流水号
            md5Map.put("ORG_SEQ", p_req9144.askNumber);
            String bgnDate = Format.DateTime14Format.format(p_req9144.getBgnDateTime());
            xmlWriter.writeElementString("ORG_TIME", bgnDate);//原交易时间
            md5Map.put("ORG_TIME", bgnDate);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String RSP_CD = tmpBkReader.getFirstText("/TXN/HEAD/RSP_CD");
                    if ("0".equals(RSP_CD)) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/TXN/CONTENT/ORG_RSP_DESC");
                        retDetail.transState = tmpBkReader.getFirstText("/TXN/CONTENT/ORG_RSP_CD");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.reqSeqID = p_req9144.askNumber;
                    } else if ("原交易不存在".equals(tmpBkReader.getFirstText("/TXN/HEAD/RSP_DESC"))) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = "原交易不存在";
                        retDetail.transState = RSP_CD;
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.reqSeqID = p_req9144.askNumber;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpBkReader.getFirstText("/TXN/HEAD/RSP_DESC");
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }

        } catch (Exception ex) {
            retDetail.respInfo = ex.getMessage();
            retDetail.respCode = "-1";
            ex.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }
}
